<template>
	<view class="content">
		<view class="card">
			<view class="price">
				<text>¥</text>
				<view>{{payData.price}}</view>
			</view>
			<view class="text">应付费用</view>
			<view class="list">
				<view class="item">
					<text>订单编号</text>
					<view>{{payData.order_sn}}</view>
				</view>
				<view class="item">
					<text>下单时间</text>
					<view>{{payData.createtime}}</view>
				</view>
				<view class="item" v-if="payData.mobile">
					<text>手机号码</text>
					<view>{{payData.mobile}}</view>
				</view>
			</view>
		</view>

		<view class="payType">
			<view class="title">请选择支付方式</view>
			<view class="type_list">
				<view class="item" @click="pay_type = 2">
					<image src="/static/order/alipay.png"></image>
					<view>支付宝支付</view>
					<uni-icons type="circle-filled" size="22" color="#1678FF" v-if="pay_type == 2"></uni-icons>
					<uni-icons type="circle" size="22" color="#979797" v-else></uni-icons>
				</view>
				<view class="item" @click="pay_type = 1">
					<image src="/static/order/wechat.png"></image>
					<view>微信支付</view>
					<uni-icons type="circle-filled" size="22" color="#1678FF" v-if="pay_type == 1"></uni-icons>
					<uni-icons type="circle" size="22" color="#979797" v-else></uni-icons>
				</view>
			</view>
		</view>

		<view class="btn">
			<button @click="toPay" :disabled="flag">立即支付</button>
		</view>

		<u-toast ref="uToast" />
	</view>
</template>

<script>
	export default {
		data() {
			return {
				pay_type: 2,
				order_id: 0,
				payData: {
					// 订单编号
					order_sn: '',
					// 下单时间
					createtime: '',
					// 手机号码
					mobile: '',
					// 订单id
					order_id: 0,
				},

				num: 0, //执行次数

				type: 0,

				flag: false,
			}
		},
		methods: {
			getOrderinfo() {
				let that = this
				let order = {
					order_id: this.order_id,
				};
				that.request.httpTokenRequest({
					url: "order/getOrderInfo",
					method: "get",
				}, order).then(function(data) {
					if (data.code == 0) {
						that.payData = data.data
					} else {
						that.$util.msg(data.msg)
					}
				})
			},
			toPay() {
				if (this.flag) {
					return this.$util.msg('请勿重复提交')
				}
				uni.showLoading({
					mask: true
				})
				let that = this
				if (!this.order_id) {
					uni.hideLoading()
					return this.$util.msg('订单编号格式有误')
				}
				this.flag = true
				let order = {
					order_id: this.order_id,
					pay_type: this.pay_type
				};
				this.request.httpTokenRequest({
					url: "pay/pay",
					method: "post"
				}, order).then(data => {
					if (data.code == 0) {
						if (that.pay_type == 2) {
							uni.requestPayment({
								provider: 'alipay',
								orderInfo: data.data.orderInfo, //微信、支付宝订单数据 【注意微信的订单信息，键值应该全部是小写，不能采用驼峰命名】
								success: function(res) {
									that.flag = false
									uni.redirectTo({
										url: '/otherpages/pay/payStatus?order_id=' + that.order_id + '&type=' + that
											.type  + '&pay_type=' + that
											.pay_type 
									})
									// that.getPayStatus() // 逻辑需处理，不要直接使用，可以加入轮询
								},
								fail: function(err) {
									that.flag = false
									uni.hideLoading()
									// console.log('fail:' + JSON.stringify(err));
								}
							});
						}
						if (that.pay_type == 1) {
							uni.requestPayment({
								provider: 'wxpay',
								orderInfo: data.data.orderInfo, //微信、支付宝订单数据 【注意微信的订单信息，键值应该全部是小写，不能采用驼峰命名】
								success: function(res) {
									that.flag = false
									uni.redirectTo({
										url: '/otherpages/pay/payStatus?order_id=' + that.order_id + '&type=' + that
											.type
									})
									// that.getPayStatus() // 逻辑需处理，不要直接使用，可以加入轮询
								},
								fail: function(err) {
									that.flag = false
									uni.hideLoading()
									// console.log('fail:' + JSON.stringify(err));
								}
							});
						}

					} else {
						uni.hideLoading()
					}
				})
			},
			getPayStatus() {
				let that = this
				let order = {
					order_id: this.order_id,
					pay_type: this.pay_type
				};
				that.request.httpTokenRequest({
					url: "pay/get_pay_status",
					method: "post",
				}, order).then(function(data) {
					if (data.code == 0) {
						uni.hideLoading()
						that.flag = false
						uni.redirectTo({
							url: '/otherpages/pay/payStatus?order_id=' + that.order_id + '&type=' + that
								.type
						})
						// return that.$util.msg(res.msg)
					} else {
						that.num++
						if (that.num < 4) {
							setTimeout(() => {
								that.getPayStatus()
							}, that.num * 1000)
						} else {
							uni.hideLoading()
							that.flag = false
							that.$refs.uToast.show({
								title: '订单查询失败',
								type: 'warning',
								back: true,
							})
						}
					}
				})
			},
		},
		onLoad(option) {
			// 订单编号
			if (option.order_id) {
				this.order_id = option.order_id
			}
			if (option.type) {
				this.type = option.type
			}
			this.getOrderinfo()
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		padding: 40rpx 30rpx 150rpx;
		box-sizing: border-box;
	}

	.card {
		height: 636rpx;
		background: url(https://qiniu-cdn.maeiyun.com/imgs/order/pay_bg.png) 0 0 no-repeat;
		background-size: 100%;
		display: flex;
		flex-direction: column;
		align-items: center;
		padding: 79rpx 30rpx 80rpx;
		box-sizing: border-box;

		.price {
			display: flex;
			align-items: flex-end;

			text {
				font-size: 28rpx;
				color: #EF5233;
				line-height: 66rpx;
			}

			view {
				font-size: 64rpx;
				font-weight: 600;
				color: #EF5233;
				line-height: 90rpx;
				margin-right: 12rpx;
			}
		}

		.text {
			font-size: 24rpx;
			color: #AEAEAE;
			line-height: 33rpx;
			margin: 20rpx 0 139rpx;
		}

		.list {
			width: 100%;

			.item {
				display: flex;
				align-items: center;
				justify-content: space-between;
				margin-bottom: 38rpx;

				text,
				view {
					font-size: 30rpx;
					color: #0F0807;
					line-height: 42rpx;
				}
			}
		}
	}

	.payType {
		background: #FFFFFF;
		border-radius: 20rpx;
		padding: 30rpx 30rpx 40rpx;
		margin-top: 30rpx;

		.title {
			font-size: 30rpx;
			color: #858585;
			line-height: 42rpx;
			margin-bottom: 31rpx;
		}

		.type_list {
			display: flex;
			align-items: center;
			justify-content: space-between;

			.item {
				width: 300rpx;
				height: 104rpx;
				padding: 0 21rpx 0 25rpx;
				box-sizing: border-box;
				background: #F5F6F7;
				border-radius: 20rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;

				image {
					width: 50rpx;
					height: 50rpx;
				}
			}
		}
	}

	.btn {
		width: 100vw;
		height: 120rpx;
		background: #FEFFFF;
		position: fixed;
		bottom: 0;
		left: 0;
		z-index: 99;
		display: flex;
		align-items: center;

		button {
			width: 690rpx;
			height: 88rpx;
			background: #EF5233;
			border-radius: 44rpx;
			font-size: 34rpx;
			color: #FFFFFF;
			line-height: 88rpx;
		}
	}
</style>